'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var rewardService = require('../../../services/rewardService');
var roleService = require('../../../services/roleService');
var sevenSoldierConf = require('../../../config/sevenSoldierConf.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 领取士兵奖励
 */
Handler.prototype.receive = function (msg, session, next) {
    var role_id = session.uid;
    var soldier_id = msg.soldier_id;
    var player_manager = new PlayerManager(role_id);
    var reward_list = [];

    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'soldier', 'vip', 'arena', 'expedition', 'build', 'soldier', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var soldier_model = player_manager.soldier;
                var role_model = player_manager.role;

                // 是否在活动期间
                if (!sevenSoldierConf.isOpen()) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_SEVEN
                    });
                }

                // 士兵是否存在
                if (!soldier_model.isHaveSoldier(soldier_id)) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                // 是否领取过奖励如果领取过返回错误，如果没有领取过记录并发放奖励
                if (!soldier_model.addReceiveRewardList(soldier_id)) {
                    return next(null, {
                        code: Code.SOLDIER_REWARD_NOT_FALL
                    });
                } else {
                    // 发放奖励
                    reward_list = sevenSoldierConf.getRewardList(soldier_id);
                    rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_COMMON_REWARD, operationType.OPR_ACT_GROW_UP_REWARD);
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
